#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#define NUM 16

//const char *fileaddr = "/home/lhr/linux/procreplace/cmd";
const char *fileaddr = "./cmd";

int main(int argc, char*argv[], char *env[])
{
    char *const _env[NUM] = {
        (char*)"VIRTUALENV=547993",
        NULL
    };
    pid_t id = fork();
    if(id == 0)
    {
        //子进程
        printf("子进程开始运行, pid: %d\n", getpid());
        sleep(3);

        char *const _argv[NUM] = {
            (char*)"ls",
            (char*)"-a",
            (char*)"-l",
            (char*)"-i",
            NULL
        };
        execle(fileaddr, "cmd", "-a", NULL, _env);
        
        //execl("/usr/bin/ls", "ls", "-a", "-l", NULL);
        //execv("/usr/bin/ls", _argv); 
        //execlp("ls", "ls", "-a", "-l", NULL);
        //execvp("ls", _argv);
        exit(1);
    }
    else 
    {
        //父进程
        printf("父进程开始运行, pid: %d\n", getpid());
        int status = 0;
        pid_t id = waitpid(-1, &status, 0);
        if(id > 0)
        {
            printf("wait success, exit code: %d\n", WEXITSTATUS(status));
        }
    }
    return 0;
}



